﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Web.Library.DomainObject.SQLServer;
using Web.Library.Facade.SQLServer;
using System.Configuration;
using Web.Support;

public partial class Form_SysUserForm : System.Web.UI.Page
{
    private SysUserFacade Facade = new SysUserFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    private SysGroupsFacade FacadeGroup = new SysGroupsFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    private SysApplicationGroupsFacade FacadeApp = new SysApplicationGroupsFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    private int id = -1;
    //private SysApplicationFacade Facade = new SysApplicationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

        }
    }

    protected override void OnLoad(EventArgs e)
    {
        try
        {
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            List<SysGroups> listGroup = new List<SysGroups>();
            listGroup = FacadeGroup.GetList();
            ddlGroup.Items.Clear();
            listGroup.ForEach(delegate(SysGroups group)
            {
                ddlGroup.Items.Add(new ListItem(group.GroupName, group.GroupID.ToString()));
            });
            ddlGroup.Items.Insert(0, new ListItem("--select group---", "0"));


            //List<SysApplicationGroups> listGroupApp = new List<SysApplicationGroups>();
            //listGroupApp = FacadeApp.GetList();
            //ddlGroupApp.Items.Clear();
            //listGroupApp.ForEach(delegate(SysApplicationGroups groupapp)
            //{
            //    ddlGroupApp.Items.Add(new ListItem(groupapp.AppGroupName, groupapp.AppGroupID.ToString()));
            //});
            //ddlGroupApp.Items.Insert(0, new ListItem("--select group app---", "0"));


            if (!Page.IsPostBack)
            {
                if (id > 0) // Edit an existing item
                {
                    SysUser obj = Facade.GetById(id);
                    if (obj != null)
                    {
                        hfID.Value = id.ToString();
                        txtName.Text = obj.Name;
                        txtEmail.Text = obj.UserCode;
                        
                        //ddlGroupApp.Items.FindByValue(obj.GroupAppID.ToString()).Selected = true;
                        ddlGroup.Items.FindByValue(obj.GroupID.ToString()).Selected = true;
                        hfGroupAppID.Value = obj.GroupAppID==null?string.Empty:obj.GroupAppID;
                    }
                }
                //GetPrivilege();
            }
        }
        catch (Exception ex)
        {
            Logger.Debug("OnLoad on sysUserForm.aspx", ex);
        }

    }
}